package com.qianli.user.application;

import com.alibaba.fastjson.JSON;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.MD5;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.xjd.user.common.constants.UserProfileConsts;
import com.fqgj.xjd.user.common.enums.UserErrorCodeEnum;
import com.fqgj.xjd.user.common.utils.WeightRandomHandler;
import com.fqgj.xjd.user.dao.*;
import com.fqgj.xjd.user.entity.*;
import com.fqgj.xjd.user.service.CheckUserService;
import com.fqgj.xjd.user.service.UserAccessService;
import com.fqgj.xjd.user.so.UserAccessToken;
import com.qianli.user.domain.enums.UserAppInfoAddType;
import com.qianli.user.domain.facatory.UserAccessFactory;
import com.qianli.user.domain.model.UserAccessModel;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.account.UserAccessRO;
import com.qianli.user.ro.account.UserLoginRO;
import com.qianli.user.ro.account.UserProductCategoryRO;
import com.qianli.user.ro.account.UserThirdPartRO;
import com.robert.vesta.util.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * name:用户登录
 * auth:ting.li@qianli-inc.com
 * date:2018/1/2 16:41
 * version: v1.0.0
 **/
@Service
public class UserAccessApplication extends UserAbstractApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserAccessApplication.class);

    @Autowired
    private UserAccessFactory userAccessFactory;
    @Autowired
    private UserBehaviorApplication userBehaviorApplication;
    @Autowired
    private UserThirdPartDao userThirdPartDao;
    @Autowired
    UserAccessDao userAccessDao;
    @Autowired
    CheckUserService checkUserService;
    @Autowired
    UserAccessService userAccessService;
    @Autowired
    private UserActionLogDao userActionLogDao;
    @Autowired
    private UserBehaviorRegisterDao userBehaviorRegisterDao;
    @Autowired
    private UserCategoryDao userCategoryDao;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private UserAuthStatusDao userAuthStatusDao;
    @Autowired
    private UserLevelInfoDao userLevelInfoDao;

    /**
     * 注册
     * 1、已存在用户抛异常；2、注册记录设备相关信息
     * @param userLogin
     * @return
     */
    public Response<UserAccessRO> register(UserLoginRO userLogin) {
        //取模型
        UserAccessModel userAccessModel = new UserAccessModel(null, userLogin.getMobile());
        userAccessModel.setAppCode(userLogin.getAppCode());
        userAccessModel.setBiz(userLogin.getBiz());
        userAccessFactory.load(userAccessModel);
        if (null != userAccessModel.getUserCode()) {
            LOGGER.info("user already register,params:" + JSON.toJSONString(userLogin));
            return Response.error(UserStateCode.USER_REGISTER_ALREADY);
        }
        //注册
        String userCode = IdUtils.genId()+"";
        userAccessModel.setUserCode(userCode);
        userAccessModel.setAppCode(userLogin.getAppCode());
        userAccessModel.setBiz(userLogin.getBiz());
        userAccessFactory.register(userAccessModel);
//        LOGGER.info("user register success,mobile:" + userAccessModel.getMobile() + ",userCode:" + userAccessModel.getUserCode());
        //注册设备信息
        userLogin.getAppInfo().setAppCode(userLogin.getAppCode());
        userBehaviorApplication.addUserAppInfo(userAccessModel.getUserCode(), userLogin.getAppInfo(),
                UserAppInfoAddType.REGISTER);
        //组装返回
        UserAccessRO userRO = new UserAccessRO();
        userRO.setBiz(userLogin.getBiz());
        userRO.setAppCode(userLogin.getAppCode());
        userRO.setUserCode(userAccessModel.getUserCode());
        userRO.setMobile(userLogin.getMobile());
        return Response.ok(userRO);
    }

    /**
     * 登录
     * 1、不自动注册，不存在用户抛异常；2、登录记录设备相关信息；
     * @param userLogin
     * @return
     */
    public Response<UserAccessRO> login(UserLoginRO userLogin) {
        //取模型
        UserAccessModel userAccessModel = new UserAccessModel(null, userLogin.getMobile());
        userAccessModel.setAppCode(userLogin.getAppCode());
        userAccessModel.setBiz(userLogin.getBiz());
        userAccessFactory.load(userAccessModel);
        if (null == userAccessModel.getUserCode()) {
//            LOGGER.info("user not register,params:" + JSON.toJSONString(userLogin));
            return Response.error(UserStateCode.USER_NOT_EXITS);
        }
        //登录
        userAccessFactory.login(userAccessModel);
//        LOGGER.info("user login success,mobile:" + userAccessModel.getMobile() + ",userCode:" + userAccessModel.getUserCode());
        //新增登录信息
        userLogin.getAppInfo().setAppCode(userLogin.getAppCode());
        userBehaviorApplication.addUserAppInfo(userAccessModel.getUserCode(), userLogin.getAppInfo(),
                UserAppInfoAddType.LOGIN);
        //组装返回
        UserAccessRO userRO = new UserAccessRO();
        userRO.setBiz(userLogin.getBiz());
        userRO.setAppCode(userLogin.getAppCode());
        userRO.setUserCode(userAccessModel.getUserCode());
        userRO.setMobile(userLogin.getMobile());
        return Response.ok(userRO);
    }

    public Response<UserAccessRO> thirdPartLogin(UserThirdPartRO userThirdPartRO) {
        UserAccessRO userAccessRO = new UserAccessRO();
        String openId = userThirdPartRO.getOpenId();
        UserThirdPartEntity userThirdPartEntity = userThirdPartDao.selectOneByOpenId(openId);
        //首次登录
        if (userThirdPartEntity == null) {
            //生成user_code
            Long sequenceNo = IdUtils.genId();
            String userCode = IdUtils.genId()+"";
            userThirdPartEntity = new UserThirdPartEntity()
                    .setUserCode(userCode)
                    .setOpenId(openId)
                    .setOrigin(userThirdPartRO.getLoginTypeEnum().name());

            userThirdPartDao.insertWithCustomId(userThirdPartEntity);

            UserAccessEntity userAccessEntity = new UserAccessEntity();
            UserAccessToken userAccessToken = new UserAccessToken(userCode);
            userAccessEntity.setUserCode(userCode);
            userAccessEntity.setAppCode(userThirdPartRO.getAppCode() + "");
            userAccessEntity.setToken(userAccessToken.getToken());
            userAccessEntity.setExpireDate(userAccessToken.getExpireDate());
            userAccessEntity.setCreateDt(new Date());
            userAccessDao.insert(userAccessEntity);

            LOGGER.info("user register by thirdPartLogin success,openId:{},type:{},user_code:{}",
                    userThirdPartRO.getOpenId(),
                    userThirdPartRO.getLoginTypeEnum(),
                    userCode);
            //注册时设备信息
            userThirdPartRO.getAppInfo().setAppCode(userThirdPartRO.getAppCode());
            userBehaviorApplication.addUserAppInfo(userCode, userThirdPartRO.getAppInfo(),
                    UserAppInfoAddType.REGISTER);
            //用户 绑定产品类目
            String productCategory = bindProductCategory(userThirdPartRO, userCode);

            userAccessRO = new UserAccessRO(userCode, "").setAccessToken(userAccessToken.getToken());
            userAccessRO.setUserCode(userCode);
            userAccessRO.setProductCategory(productCategory);
        } else {
            String userCode = userThirdPartEntity.getUserCode();
            UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);

            UserAccessToken userAccessToken = userAccessService.refreshUserAccessToken(userAccessEntity.getUserCode());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("userCode", userCode);
            List<UserCategoryEntity> userCategoryEntities = userCategoryDao.selectByParams(paramMap);

            //此时有可能用户还没有绑定手机号
            userAccessRO = new UserAccessRO(userCode, userAccessEntity.getMobile());
            userAccessRO.setAccessToken(userAccessToken.getToken());
            userAccessRO.setUserCode(userCode);
            userAccessRO.setProductCategory(userCategoryEntities.get(userCategoryEntities.size() - 1).getCategoryCode());
            //登录时设备信息
            userThirdPartRO.getAppInfo().setAppCode(userThirdPartRO.getAppCode());
            userBehaviorApplication.addUserAppInfo(userCode, userThirdPartRO.getAppInfo(),
                    UserAppInfoAddType.LOGIN);
        }
        return Response.ok().putData(userAccessRO);
    }

    public Response<UserAccessRO> thirdSocialPartLogin(UserThirdPartRO userThirdPartRO) {
        UserAccessRO userAccessRO = new UserAccessRO();
        String openId = userThirdPartRO.getOpenId();
        UserThirdPartEntity userThirdPartEntity = userThirdPartDao.selectOneByOpenId(openId);
        //首次登录
        if (userThirdPartEntity == null) {
            userThirdPartEntity = new UserThirdPartEntity()
                    .setUserCode(null)
                    .setOpenId(openId)
                    .setOrigin(userThirdPartRO.getLoginTypeEnum().name());
            userThirdPartDao.insertWithCustomId(userThirdPartEntity);
        } else {
            String userCode = userThirdPartEntity.getUserCode();
            if(StringUtils.isNotEmpty(userCode)){//三方账号有记录但未绑定手机号
                UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
                UserAccessToken userAccessToken = userAccessService.refreshUserAccessToken(userAccessEntity.getUserCode());
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("userCode", userCode);
                List<UserCategoryEntity> userCategoryEntities = userCategoryDao.selectByParams(paramMap);
                //此时有可能用户还没有绑定手机号
                userAccessRO = new UserAccessRO(userCode, userAccessEntity.getMobile());
                userAccessRO.setAccessToken(userAccessToken.getToken());
                userAccessRO.setUserCode(userCode);
                userAccessRO.setProductCategory(userCategoryEntities.get(userCategoryEntities.size() - 1).getCategoryCode());
                //登录时设备信息
                userThirdPartRO.getAppInfo().setAppCode(userThirdPartRO.getAppCode());
                userBehaviorApplication.addUserAppInfo(userCode, userThirdPartRO.getAppInfo(),
                        UserAppInfoAddType.LOGIN);
            }
        }
        return Response.ok().putData(userAccessRO);
    }

    /**
     * 绑定产品类目
     */
    private String bindProductCategory(UserThirdPartRO userThirdPartRO, String userCode) {
        List<UserProductCategoryRO> userProductCategoryROList = userThirdPartRO.getUserProductCategoryROList();
        //1. 遍历 产品类目列表  剔除达到阈值的产品类目
        Iterator<UserProductCategoryRO> iterator = userProductCategoryROList.iterator();
        while (iterator.hasNext()) {
            UserProductCategoryRO userProductCategoryRO = iterator.next();
            String productCategoryName = userProductCategoryRO.getProductCategoryName();
            Long number = redisClient.getNumber(UserProfileConsts.DAILY_REGISTER_USER_LIMIT_ORGANIC, productCategoryName, DateUtil.getDate("yyyy-MM-dd"));
            if (number != null && userProductCategoryRO.getDailyRegisterUserLimit() != null && number >= userProductCategoryRO.getDailyRegisterUserLimit()) {
                iterator.remove();
            }
        }
        //2. 其他的 筛选出一个
        List<String> productCategoryNameList = userProductCategoryROList.stream().map(UserProductCategoryRO::getProductCategoryName).collect(Collectors.toList());
        double[] weightArrays = new double[productCategoryNameList.size()];
        for (int i = 0; i < productCategoryNameList.size(); i++) {
            //权重一样
            weightArrays[i] = 1;
        }
        int weightValue = WeightRandomHandler.getWeightRandom(weightArrays);
        String productCategoryName = productCategoryNameList.get(weightValue);
        //3. 设置 并 自增
        redisClient.incrBy(UserProfileConsts.DAILY_REGISTER_USER_LIMIT_ORGANIC, 1L, productCategoryName, DateUtil.getDate("yyyy-MM-dd"));
        List<UserCategoryEntity> userCategoryEntities = userCategoryDao.selectListByUserCode(userCode);
        if(CollectionUtils.isNotEmpty(userCategoryEntities)){
            return userCategoryEntities.get(0).getCategoryCode();
        }
        //4. 保存
        UserCategoryEntity userCategoryEntity = new UserCategoryEntity();
        userCategoryEntity.setUserCode(userCode);
        userCategoryEntity.setCategoryCode(productCategoryName);
        userCategoryDao.insert(userCategoryEntity);
        LOGGER.info("用户绑定产品类目 userCode:{},productCategoryName:{}", userCode, productCategoryName);
        return productCategoryName;
    }

    public Response<String> bindUserMobileByOpenId(UserThirdPartRO userThirdPart) {
        UserThirdPartEntity userThirdPartEntity = userThirdPartDao.selectOneByOpenId(userThirdPart.getOpenId());
        if (userThirdPartEntity == null || !userThirdPartEntity.getOrigin().equals(userThirdPart.getLoginTypeEnum().name())) {
            return Response.error(UserErrorCodeEnum.USER_NOT_EXIST, "三方登陆信息不存在");
        }

        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userThirdPartEntity.getUserCode());
        if (userAccessEntity == null) {
            return Response.error(UserErrorCodeEnum.USER_NOT_EXIST);
        }
        if (Objects.equals(userAccessEntity.getMobile(), userThirdPart.getMobile())) {
            return Response.ok().putData(userThirdPart.getMobile());
        }
        //判断此手机号是否存在
        //if (checkUserService.isMobileExist(new Mobile(userThirdPart.getMobile()), null)) {
        //
        //
        //    return Response.error(UserErrorCodeEnum.MOBILE_EXIST, UserErrorCodeEnum.MOBILE_EXIST.getMsg());
        //}

        //判断此手机号是否存在 因为旧的应用被facebook删除导致openid变动 需要 依靠手机号绑定旧用户
        UserAccessEntity userByMobile = checkUserService.getUserByMobile(userThirdPart.getMobile(), userThirdPart.getAppCode());

        //把用户在绑定手机号之前有过H5的渠道记录
        try {
            List<String> mobiles = new ArrayList<>();
            mobiles.add(userThirdPart.getMobile());
            List<UserActionLogEntity> data = userActionLogDao.selectListByTypeAndValues(1, mobiles);
            LOGGER.info("== 更新pre手机号码 ==,mobile:{}", userThirdPart.getMobile());
            if (!CollectionUtils.isEmpty(data)) {
                Map<String, UserActionLogEntity> mobileAndChannelMap = new HashMap<>();
                for (UserActionLogEntity rs : data) {
                    mobileAndChannelMap.put(rs.getActionValue(), rs);
                }

                UserActionLogEntity preChannel = mobileAndChannelMap.get(userThirdPart.getMobile());
                LOGGER.info("== 更新pre手机号码 ==,mobile:{},preChannel:{}", userThirdPart.getMobile(), preChannel == null ? "为空" : preChannel);

                if (preChannel != null) {
                    UserBehaviorRegisterEntity userBehaviorRegisterEntity = userBehaviorRegisterDao.selectUserBehaviorRegisterByUserCodeAndAppCode(userByMobile.getUserCode());
                    if (userBehaviorRegisterEntity != null) {
                        LOGGER.info("== 更新pre手机号码 ==,mobile:{},preDate:{},afterDate:{}", userThirdPart.getMobile(), preChannel.getGmtCreate(), userBehaviorRegisterEntity.getGmtCreate());
                        if (userBehaviorRegisterEntity.getGmtCreate().after(preChannel.getGmtCreate())) {
                            LOGGER.info("== 更新pre手机号码 update==,mobile:{},preDate:{},afterDate:{}", userThirdPart.getMobile(), preChannel.getGmtCreate(), userBehaviorRegisterEntity.getGmtCreate());
                            userBehaviorRegisterEntity.setChannel(preChannel.getChannel());
                            userBehaviorRegisterDao.updateByPrimaryKey(userBehaviorRegisterEntity);
                        }
                    } else {
                        LOGGER.info("== 更新pre手机号码 userBehaviorRegisterEntity为空==,mobile:{}", userThirdPart.getMobile());
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.info("== 修复pre的channel发生错误 ==,userThirdPart:{}", JSON.toJSONString(userThirdPart), e);
        }

        //手机号一样 但是usercode不一样 将新的openid 和新的token更新给老用户
        if (null != userByMobile && !userAccessEntity.getUserCode().equals(userByMobile.getUserCode())) {
            LOGGER.info("旧应用数据迁移,old:{},new:{}", JSON.toJSONString(userByMobile), JSON.toJSONString(userThirdPart));
            //使新的openid 对应旧的userCode
            userThirdPartDao.updateUserCodeByOpenId(userByMobile.getUserCode(), userThirdPart.getOpenId());
            //使此用户的token对应旧的userCode
            userAccessService.moveTokenToOldUser(userByMobile, userAccessEntity);
        } else {
            userAccessEntity.setMobile(userThirdPart.getMobile());
            userAccessDao.updateByPrimaryKey(userAccessEntity);
        }
        return Response.ok().putData(userThirdPart.getMobile());
    }


    public Response<UserAccessRO> bindUserMobileBySocialOpenId(UserThirdPartRO userThirdPart) {
        UserThirdPartEntity userThirdPartEntity = userThirdPartDao.selectOneByOpenId(userThirdPart.getOpenId());
        if (userThirdPartEntity == null || !userThirdPartEntity.getOrigin().equals(userThirdPart.getLoginTypeEnum().name())) {
            return Response.error(UserErrorCodeEnum.USER_NOT_EXIST, "Facebook account not bound");
        }
        UserAccessRO userAccessRO = new UserAccessRO();
        if(StringUtils.isNotEmpty(userThirdPartEntity.getUserCode())){
            UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userThirdPartEntity.getUserCode());
            if (userAccessEntity == null) {
                return Response.error(UserErrorCodeEnum.USER_NOT_EXIST);
            }
            userAccessRO.setAccessToken(userAccessEntity.getToken());
            userAccessRO.setMobile(userAccessEntity.getMobile());
            userAccessRO.setCustomerCode(userAccessEntity.getCustomerCode());
            if (Objects.equals(userAccessEntity.getMobile(), userThirdPart.getMobile())) {
                return Response.ok().putData(userAccessRO);
            }
        }else{
            UserAccessEntity userAccessEntity = userAccessDao.selectUserByMobileAndAppCode(userThirdPart.getMobile(), userThirdPart.getAppCode());
            if(userAccessEntity != null){
                userThirdPartEntity.setUserCode(userAccessEntity.getUserCode());
                UserAccessToken userAccessToken = userAccessService.refreshUserAccessToken(userAccessEntity.getUserCode());

                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("userCode", userAccessEntity.getUserCode());
                List<UserCategoryEntity> userCategoryEntities = userCategoryDao.selectByParams(paramMap);

                //此时有可能用户还没有绑定手机号
                userAccessRO = new UserAccessRO(userAccessEntity.getUserCode(), userAccessEntity.getMobile());
                userAccessRO.setAccessToken(userAccessToken.getToken());
                userAccessRO.setUserCode(userAccessEntity.getUserCode());
                userAccessRO.setProductCategory(userCategoryEntities.get(userCategoryEntities.size() - 1).getCategoryCode());
                //登录时设备信息
                userThirdPart.getAppInfo().setAppCode(userThirdPart.getAppCode());
                String productCategory = bindProductCategory(userThirdPart, userAccessEntity.getUserCode());
                userAccessRO = new UserAccessRO(userAccessEntity.getUserCode(), userThirdPart.getMobile()).setAccessToken(userAccessToken.getToken());
                userAccessRO.setUserCode(userAccessEntity.getUserCode());
                userAccessRO.setProductCategory(productCategory);
            }else{
                //生成user_code
                Long sequenceNo = IdUtils.genId();
                String userCode = IdUtils.genId()+"";
                UserAccessToken userAccessToken = new UserAccessToken(userCode);
                UserAccessEntity userAccess = new UserAccessEntity();
                userAccess.setUserCode(userCode);
                userAccess.setAppCode(userThirdPart.getAppCode() + "");
                userAccess.setToken(userAccessToken.getToken());
                userAccess.setExpireDate(userAccessToken.getExpireDate());
                userAccess.setCreateDt(new Date());
                userAccess.setMobile(userThirdPart.getMobile());
                userAccess.setMd5Mobile(MD5.md5(userThirdPart.getMobile()));
                userAccess.setPackageName(userThirdPart.getAppInfo().getPackageName());
                userAccessDao.insert(userAccess);

                LOGGER.info("user register by thirdPartLogin success,openId:{},type:{},user_code:{}",
                        userThirdPart.getOpenId(),
                        userThirdPart.getLoginTypeEnum(),
                        userCode);
                //注册时设备信息
                userThirdPart.getAppInfo().setAppCode(userThirdPart.getAppCode());
                userBehaviorApplication.addUserAppInfo(userCode, userThirdPart.getAppInfo(),
                        UserAppInfoAddType.REGISTER);
                //用户 绑定产品类目
                String productCategory = bindProductCategory(userThirdPart, userCode);

                //更新状态表
                UserAuthStatusEntity userAuthStatusEntity = new UserAuthStatusEntity();
                userAuthStatusEntity.setUserCode(userCode);
                userAuthStatusDao.insert(userAuthStatusEntity);
                //用户权益
                UserLevelInfoEntity userLevelInfoEntity = new UserLevelInfoEntity()
                        .setLevelCode("0")
                        .setUserCode(userCode)
                        .setCreateDate(new Date())
                        .setScore(0)
                        .setTotalReward(0);
                userLevelInfoDao.insert(userLevelInfoEntity);

                userAccessRO = new UserAccessRO(userCode, userThirdPart.getMobile()).setAccessToken(userAccessToken.getToken());
                userAccessRO.setUserCode(userCode);
                userAccessRO.setProductCategory(productCategory);
                userThirdPartEntity.setUserCode(userCode);
            }
            userThirdPartDao.updateByPrimaryKey(userThirdPartEntity);
        }

        //把用户在绑定手机号之前有过H5的渠道记录
        try {
            List<String> mobiles = new ArrayList<>();
            mobiles.add(userThirdPart.getMobile());
            List<UserActionLogEntity> data = userActionLogDao.selectListByTypeAndValues(1, mobiles);
            LOGGER.info("== 更新pre手机号码 ==,mobile:{}", userThirdPart.getMobile());
            if (!CollectionUtils.isEmpty(data)) {
                Map<String, UserActionLogEntity> mobileAndChannelMap = new HashMap<>();
                for (UserActionLogEntity rs : data) {
                    mobileAndChannelMap.put(rs.getActionValue(), rs);
                }

                UserActionLogEntity preChannel = mobileAndChannelMap.get(userThirdPart.getMobile());
                LOGGER.info("== 更新pre手机号码 ==,mobile:{},preChannel:{}", userThirdPart.getMobile(), preChannel == null ? "为空" : preChannel);

                if (preChannel != null) {
                    UserBehaviorRegisterEntity userBehaviorRegisterEntity = userBehaviorRegisterDao.selectUserBehaviorRegisterByUserCodeAndAppCode(userThirdPartEntity.getUserCode());
                    if (userBehaviorRegisterEntity != null) {
                        LOGGER.info("== 更新pre手机号码 ==,mobile:{},preDate:{},afterDate:{}", userThirdPart.getMobile(), preChannel.getGmtCreate(), userBehaviorRegisterEntity.getGmtCreate());
                        if (userBehaviorRegisterEntity.getGmtCreate().after(preChannel.getGmtCreate())) {
                            LOGGER.info("== 更新pre手机号码 update==,mobile:{},preDate:{},afterDate:{}", userThirdPart.getMobile(), preChannel.getGmtCreate(), userBehaviorRegisterEntity.getGmtCreate());
                            userBehaviorRegisterEntity.setChannel(preChannel.getChannel());
                            userBehaviorRegisterDao.updateByPrimaryKey(userBehaviorRegisterEntity);
                        }
                    } else {
                        LOGGER.info("== 更新pre手机号码 userBehaviorRegisterEntity为空==,mobile:{}", userThirdPart.getMobile());
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.info("== 修复pre的channel发生错误 ==,userThirdPart:{}", JSON.toJSONString(userThirdPart), e);
        }
        return Response.ok().putData(userAccessRO);
    }


    /**
     * 查询账号
     * @param model
     * @return
     */
    public Response<UserAccessRO> queryUserAccess(UserAccessModel model) {
        userAccessFactory.load(model);
        //if (!(null != model.getMobile() && null == model.getUserCode())) {
        //    LOGGER.info("UserAccessApplication.queryUserAccess user not exists{}" + JSON.toJSONString(model));
        //    return Response.error(UserStateCode.USER_NOT_EXITS);
        //}
        UserAccessRO userAccess = new UserAccessRO();
        userAccess.setUserCode(model.getUserCode());
        userAccess.setMobile(model.getMobile());
        userAccess.setCustomerCode(model.getCustomerCode());
        userAccess.setBiz(model.getBiz());
        userAccess.setAppCode(model.getAppCode());
        return Response.ok(userAccess);
    }
}
